package com.woniu.controller;

import com.woniu.pojo.Chapter;
import com.woniu.service.IChapterService;
import com.woniu.util.Result;
import com.woniu.vo.ChapterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiaoruan
 * @since 2023-03-15
 */

@Api(tags = "章节管理")
@Slf4j
@RestController
@RequestMapping("/chapter")
public class ChapterController extends BaseController {
    @Autowired
    IChapterService chapterService;

    @ApiOperation("根据课程id查询对应的章节数据")
    @GetMapping("find/{courseId}")
    public Result findChaterVOById(@PathVariable String courseId) {
        List<ChapterVO> chapterList = chapterService.findChaterBycourseID(courseId);

        return toDataResult(chapterList);
    }


    @ApiOperation("根据id删除")
    @DeleteMapping("/delete/{id}")
    public Result deleteChapter(@PathVariable String id) {

        chapterService.deleteById(id);

        return success();
    }


    @ApiOperation("新增或者修改")
    @PostMapping("/addOrUpdate")
    public Result addChapter(@RequestBody Chapter chapter) {

        chapterService.saveOrUpdate(chapter);

        return success();

    }

    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public Result getChapterById(@PathVariable String id) {
        Chapter chapter = chapterService.getById(id);

        return toDataResult(chapter);
    }


    @ApiOperation("根据couseId查询sort排序最大值")
    @GetMapping("/maxsort/{courseId}")
    public Result getsortById(@PathVariable String courseId) {
       Integer sort = chapterService.getMaxSort(courseId);

        return toDataResult(sort);
    }




}
